
const request = require('../../../../utils/request');
var page = 1, num = 10, type = 'distance';

function loadingData(that,page,num,rid){
    if(that.data.upflag){
        if(rid == undefined){
            rid = ''
        }
        request.HttpRequst(true,'list',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":page,"num": num,"type": type,'type_rid':rid},"GET",false,res => {
            //console.log(res.data)
            if(res.data != ''){
                let goodsData = that.data.goods.concat(res.data)
                that.setData({
                    goods: goodsData
                })
            }else{
                that.setData({
                    upflag: false
                })
            }
        })
    }  
}

Page({
    data: {
        list:[0,1,2,3,4],
        goods: [],
        latitude: 0,    //纬度
        longitude: 0, //经度
        upflag: true,
        menuIndex:1,
        imgUrl:request.imgUrl,
        array: ['粤菜','川菜','鲁菜'],  //菜式分类
        index:0,
        rid: '' //菜式分类标志
    },
    onLoad: function() {
        page = 1;
        this.setData({
            latitude: wx.getStorageSync('latitude'),    //纬度
            longitude: wx.getStorageSync('longitude')  //经度
        })
        request.HttpRequst(true,'list',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {
            this.setData({
                goods: res.data
            })
        })

        //获取菜式分类
        request.HttpRequst(true,'typeReserveData',false,'','',"GET",false,res => {
            console.log('菜式分类：',res);
            this.setData({
                array: res.data
            })
        })
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '排队就餐'
        })
    },
    goShop(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../advance/shop/shop?id='+id
        })
    },
    /*下拉获取更多电影数据*/
    onReachBottom:function(){
        var that = this;
        page++;
        loadingData(that,page,num,that.data.rid);
    },

    //附近数据处理
    getDistance () {
        page = 1
        type = 'distance'
        request.HttpRequst(true,'list',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:1
            })
        })
    },
    //销量数据处理
    getSales () {
        page = 1
        type = 'sales'
        request.HttpRequst(true,'list',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:2,
                rid: ''
            })
        })
    },
    //评分数据处理
    getScore () {
        page = 1
        type = 'score'
        request.HttpRequst(true,'list',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:3,
                rid: ''
            })
        })
    },

    //菜式分类
    bindPickerChange(e) {
        console.log(e)
        type = 'distance';
        let rid = this.data.array[e.detail.value].id;
        if(e.detail.value == 0){
            rid = '';
        }
        this.setData({
            index: e.detail.value,
            page:1,
            menuIndex:1,
            rid: rid
        })
        request.HttpRequst(true,'reserve',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":this.data.page,"num": num,"type": type,'type_rid':rid},"GET",false,res => {
            console.log(res)                 
            this.setData({
                goods: res.data,
                upflag: true
            })
        })
    }
})